package com.skydroid.h30

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.skydroid.h30.view.MyVideoView
import com.skydroid.h30.view.VideoListener
import tv.danmaku.ijk.media.player.IMediaPlayer
import java.io.IOException

/**
 * RTSP视频播放
 */
class VideoActivity: AppCompatActivity(), VideoListener {

    private val PATH = "rtsp://192.168.0.10:8554/H264Video"
    private var videoView: MyVideoView? = null
    private var mContext: Context? = null
    private var mediacodec: Int? = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_video)
        this.mContext = this
        parseIntent()
        initView()
    }

    companion object{
        fun start(context: Context, mediacodec:Int){
            var intent = Intent(context,
                VideoActivity::class.java)
            intent.putExtra("mediacodec",mediacodec)
            context.startActivity(intent)
        }
    }

    private fun parseIntent(){
        mediacodec = intent.getIntExtra("mediacodec",0)
    }

    private fun initView(){
        videoView = findViewById(R.id.video)
        videoView?.setMediacodec(mediacodec?:0)
        videoView?.setVideoListener(this)
        videoView?.setPath(PATH)
        try {
            videoView?.load()
        } catch (e: IOException) {
            e.printStackTrace()
        }

    }


    override fun onSeekComplete(p0: IMediaPlayer?) {
    }

    override fun onInfo(p0: IMediaPlayer?, p1: Int, p2: Int): Boolean {
        return false
    }

    override fun onVideoSizeChanged(p0: IMediaPlayer?, p1: Int, p2: Int, p3: Int, p4: Int) {
    }

    override fun onBufferingUpdate(p0: IMediaPlayer?, p1: Int) {
    }

    override fun onPrepared(p0: IMediaPlayer?) {
        videoView?.start()
    }

    override fun onCompletion(p0: IMediaPlayer?) {
    }

    override fun onError(p0: IMediaPlayer?, p1: Int, p2: Int): Boolean {
        if(p1 == -10000 && p2 == 0){
            videoView?.load()
        }
        return false
    }

    override fun onDestroy() {
        super.onDestroy()
        videoView?.stop()
    }
}